package com.bdmc.device.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdmc.common.core.domain.PageQuery;
import com.bdmc.common.core.mybatisplus.core.ServicePlusImpl;
import com.bdmc.common.core.page.TableDataInfo;
import com.bdmc.common.utils.StringUtils;
import com.bdmc.device.domain.DevGlucoseData;
import com.bdmc.device.domain.bo.DevGlucoseDataBo;
import com.bdmc.device.domain.vo.DevGlucoseDataVo;
import com.bdmc.device.mapper.DevGlucoseDataMapper;
import com.bdmc.device.service.IDevGlucoseDataService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

/**
 * 血糖数据Service业务层处理
 *
 * @author bdmc
 * @date 2023-07-31
 */
@Service
public class DevGlucoseDataServiceImpl extends ServicePlusImpl<DevGlucoseDataMapper, DevGlucoseData, DevGlucoseDataVo> implements IDevGlucoseDataService {

    @Override
    public DevGlucoseDataVo queryById(Long glucoseId){
        return getVoById(glucoseId);
    }

    @Override
    public TableDataInfo<DevGlucoseDataVo> queryPageList(DevGlucoseDataBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<DevGlucoseData> lqw = buildQueryWrapper(bo);
        Page<DevGlucoseDataVo> result = pageVo(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public TableDataInfo<DevGlucoseDataVo> mySelectPage(DevGlucoseDataBo bo, PageQuery pageQuery) {
        return TableDataInfo.build(baseMapper.mySelectPage(pageQuery.build(),bo));
    }

    @Override
    public List<DevGlucoseDataVo> queryList(DevGlucoseDataBo bo) {
        LambdaQueryWrapper<DevGlucoseData> lqw = buildQueryWrapper(bo);
        return listVo(lqw);
    }

    private LambdaQueryWrapper<DevGlucoseData> buildQueryWrapper(DevGlucoseDataBo bo) {
        LambdaQueryWrapper<DevGlucoseData> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getImei()), DevGlucoseData::getImei, bo.getImei());
        lqw.eq(bo.getBloodGlu() != null, DevGlucoseData::getBloodGlu, bo.getBloodGlu());
        lqw.eq(StringUtils.isNotBlank(bo.getBgState()), DevGlucoseData::getBgState, bo.getBgState());
        lqw.eq(StringUtils.isNotBlank(bo.getTestTime()), DevGlucoseData::getTestTime, bo.getTestTime());
        return lqw;
    }

    @Override
    public Boolean insertByBo(DevGlucoseDataBo bo) {
        DevGlucoseData add = BeanUtil.toBean(bo, DevGlucoseData.class);
        validEntityBeforeSave(add);
        boolean flag = save(add);
        if (flag) {
            bo.setGlucoseId(add.getGlucoseId());
        }
        return flag;
    }

    @Override
    public Boolean updateByBo(DevGlucoseDataBo bo) {
        DevGlucoseData update = BeanUtil.toBean(bo, DevGlucoseData.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(DevGlucoseData entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }

    @Override
    public DevGlucoseDataVo getVoByImei(String imei) {
        LambdaQueryWrapper<DevGlucoseData> lqw = Wrappers.lambdaQuery();
        lqw.eq(DevGlucoseData::getImei,imei).orderByDesc(DevGlucoseData::getGlucoseId).last(" limit 1");
        return getVoOne(lqw);
    }
}
